<?php
class Pothole extends AppModel {
// Define the model and it's associations:
var $name = 'Pothole';
var $hasMany = array(
					'Comment' => array(
						'className' => 'Comment',
						'foreignKey' => 'object_ID',
						'conditions' => array('Comment.status' => '1'),
						'dependent' => 'true',
						'order' => 'Comment.created DESC'
					),
					'Rating' => array(
						'className' => 'Rating',
						'foreignKey' => 'object_ID',
						'dependent' => 'true',
					)
				);
var $belongsTo = array(
					'Town' => array (
						'className' => 'Town',
						'foreignKey' => 'town_ID'
						)
					);

	/*
	/	getPotholes: get all potholes within a town ID
	/	string $town_id: ID from the Towns table
	/	string $order: what field to order by
	/	string $sort: ascending or descending
	*/
	public function getPotholes($town_id = '', $order = 'modified', $sort = 'ASC') {
		$conditions = array();
		if($town_id) { $conditions['Pothole.town_id'] = $town_id; }
		$order = 'Pothole.'.$order.' '.$sort;
		$potholes = $this->find('all', array(
			'conditions' => $conditions,
			'order' => $order));
		return $potholes;
	}
	
	
	/*
	/	getPotholesByMetro: get all potholes within a metro ID
	/	string $metro_id: ID from the Towns table
	/	string $order: what field to order by
	/	string $sort: ascending or descending
	*/
	public function getPotholesByMetro($metro_id = '', $order = 'modified', $sort = 'ASC') {
		$conditions = array();
		if($metro_id) { 
			$conditions = array(
				'Town.metro_ID' => $metro_id);
			$order = 'Pothole.'.$order.' '.$sort;
			$potholes = $this->find('all', array(
				'conditions' => $conditions,
				'order' => $order));
			return $potholes;
		}
	}
	
	/*
	/	fetchPothole: info on one pothole
	/	string $pothole_id: ID from the Towns table
	*/
	public function fetchPothole($pothole_id) {
		$pothole = $this->find('first', array('conditions' => array('Pothole.ID' => $pothole_id)));
		return $pothole;
	}
	
	/*
	/	fetchPotholeInfoByName: fetch Pothole ID by it's name
	/	string $pothole: pothole name
	*/
	public function fetchPotholeInfoByName($pothole) {
		$potholeinfo = $this->find('first', array(
			'conditions' => array(
				'Pothole.name' => $pothole)/*,
			'fields' => array(
				'Metro.ID')*/));
		return $potholeinfo;
	}
	
	/*
	/	countPotholes: Counts the number of currently-active potholes
	/		in a given town.
	/	string $town_id: ID from the Towns table
	*/
	public function countPotholes($town_id) {
		$count = $this->find('count', array('conditions' => array(
			'Pothole.status NOT LIKE' => 'Closed',
			'Pothole.town_ID' => $town_id)));
		return $count;
	}
}
?>
